<?php
namespace app\common\entity;

use think\Db;
use think\Model;
use app\common\entity\LegalConfig;
use app\common\entity\LegalReturnlog;
use app\common\entity\User;


class LegalReportcentre extends Model
{
    protected $table = 'legal_reportcentre';

    const status_f1 = -1; //报单失败
    const status_0 = 0; // 等待审批
    const status_1 = 1; //报单成功
  


    public function getStatus()
    {
        switch ($this->status) {
            case self::status_f1:
                return '报单失败';
            case self::status_0:
                return '等待审批';
            case self::status_1:
                return '报单成功';
        }
    }


    public function returnmoney()
    {   
        //查询所有会员
        $alluser = User::where('coin', '>',0)->where('cointime', '>',0)->select();
        
        $LegalConfig = new LegalConfig();

        //释放比例
        $returnratio = $LegalConfig->getValue('releaseratio');

        $User = new User();
       
        $LegalReturnlog = new LegalReturnlog();

        
        //查看今天运行了没
        $isfa = $LegalReturnlog->order('id','desc')->find();
        if($isfa['createtime'] > strtotime(date('Y-m-d')) ){
            echo '今天已发';
            return;
        }


        foreach ($alluser as $value) {

            $returnmoney = floor($value['coin'] * $returnratio) / 100;
            $arr = [
                'user_id'=> $value['id'],
                'returnmoney'=> $returnmoney,
                'returnratio'=>$returnratio,
                'qian'=>$value['coin'],
                'hou'=> $value['coin']-$returnmoney,
                'surplusday'=>$value['cointime']-1,
                'createtime'=>time()
            ];

            $LegalReturnlog = new LegalReturnlog();
            $LegalReturnlog->save($arr);

            //减少币种
            $User->setBonus($value['id'],'coin',-$returnmoney,'每日返减少');

            //增加数字资产
            $User->setBonus($value['id'],'property',$returnmoney,'每日返增加');

            //减少待返时间
            $User->setBonus($value['id'],'cointime',-1,'每日返减少');

        }

        unset($value);
        echo '执行成功';
    }







}
